<?php

namespace App\Validators\Student;

use App\Validators\PassesValidator;
use Illuminate\Support\Facades\Input;
use \Prettus\Validator\Contracts\ValidatorInterface;
use \Prettus\Validator\LaravelValidator;

class StudentValidator extends LaravelValidator
{
    use PassesValidator;
    protected $rulesMessages=[
        'name.required' => '姓名是必填的',
        'id_card.required' => '身份证号是必填的',
        'id_card.unique' => '身份证号已经存在',
        'census_no.unique' => '学籍号已经存在',
        'census_no.required'  => '学籍号是必填的',
        'born_date.required'  => '出生日期是必填的',
        'hometown.required'  => '出生地是必填的',
        'citizenship.required'  => '国家是必填的',
        'home_address.required'  => '家庭地址是必填的',
        'now_address.required'  => '现居地址是必填的',
        'id.not_exists'  => '该学生信息不能被删除',
    ];

    protected $rules = [
        ValidatorInterface::RULE_CREATE => [
            'name'=>'required',
            'id_card'=>'required|unique:student',
            'census_no'=>'required|unique:student',
            'born_date'=>'required',
            'hometown'=>'required',
            'citizenship'=>'required',
            'home_address'=>'required',
            'now_address'=>'required',],
        ValidatorInterface::RULE_UPDATE => [
            'name'=>'required',
            'id_card'=>'required|unique:student,id_card,{id}',
            'census_no'=>'required|unique:student,census_no,{id}',
            'born_date'=>'required',
            'hometown'=>'required',
            'citizenship'=>'required',
            'home_address'=>'required',
            'now_address'=>'required',
        ],
        'DELETE'=>[
            'id'=>'not_exists:student_resume,student_id|not_exists:student_school_census_change,student_id|not_exists:student_relation,student_id',
        ]
   ];

    public function callBackGetRules(array $rules){
        $result=[];
        $value_change = array('{id}'=>Input::get('id'));
        foreach ($rules as $key=>$value){
            if(is_string($value) && str_contains($value,'{id}')){
                $result[$key]=strtr($value,$value_change);
            }else{
                $result[$key]=$value;
            }
        }
        return $result;
    }
}
